package com.wanlian.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.wanlian.dao.AuthorityDao;
import com.wanlian.dao.UserAuthorityDao;
import com.wanlian.entity.Authority;
import com.wanlian.entity.UserAuthority;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.model.PageResult;
import com.wanlian.service.UserAuthorityServices;
import com.wanlian.vo.Page;
import com.wanlian.vo.UserAuthorityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
public class UserAuthorityServiceImpl implements UserAuthorityServices
{

    @Autowired
    private UserAuthorityDao userAuthorityDao;

    @Autowired
    private AuthorityDao authorityDao;

    @Override
    public void add(UserAuthority userAuthority)
    {
        UserAuthority isExist = userAuthorityDao.getIsExistUserAuthority(userAuthority.getUserid(), userAuthority.getAuthorityid());
        if (isExist != null)
        {
            return;
        }
        userAuthorityDao.add(userAuthority);
    }

    @Override
    public void doedit(UserAuthority userAuthority)
    {
        userAuthorityDao.doedit(userAuthority);
    }

    @Override
    public void delete(int id)
    {
        userAuthorityDao.delete(id);
    }

    @Override
    public UserAuthority getUserAuthority(int id)
    {
        return userAuthorityDao.getUserAuthority(id);
    }

    @Override
    public List<UserAuthority> queryUserAuthority(int userid)
    {
        return userAuthorityDao.queryUserAuthority(userid);
    }

    @Override
    public List<UserAuthorityVo> queryUserAuthorityVo(int userid)
    {
        List<UserAuthority> list = userAuthorityDao.queryUserAuthority(userid);
        List<UserAuthorityVo> voList = new ArrayList<>();
        if (list.size() > 0)
        {
            voList = JsonStringUtil.readValueByList(JsonStringUtil.writeValueAsString(list), UserAuthorityVo.class);
            for (UserAuthorityVo userAuthorityVo : voList)
            {
                Authority auth = authorityDao.queryById(userAuthorityVo.getAuthorityid());
                if (auth != null)
                {
                    userAuthorityVo.setAuthorityname(auth.getAuthorityname());
                    userAuthorityVo.setAuthorityurl(auth.getMethodorurl());
                }
            }
        }
        return voList;
    }

    @Override
    public UserAuthority getIsExistUserAuthority(int userid, int authorityid)
    {
        return userAuthorityDao.getIsExistUserAuthority(userid, authorityid);
    }

    @Override
    public PageResult queryPageList(UserAuthority userAuthority, Page page)
    {
        return userAuthorityDao.queryPageList(userAuthority, page);
    }

    @Override
    public List<UserAuthority> queryByAuthorityid(int authorityid)
    {
        return userAuthorityDao.queryByAuthorityid(authorityid);
    }

    @Override
    public void deleteByUserId(int userId)
    {
        userAuthorityDao.deleteByUserId(userId);
    }

}
